﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class GopY : Form
    {
        public GopY()
        {
            InitializeComponent();
        }

        private void GopY_Load(object sender, EventArgs e)
        {
            LoadDataGridViewGopY();
        }

        private void LoadDataGridViewGopY()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from GopY";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "GopY");
            dataGridViewGopY.AutoGenerateColumns = false;
            dataGridViewGopY.DataSource = ds.Tables["GopY"];
            conn.Close();
        }

        private void deleteGopY(string idtgy, string htngy, string dc, string sdt, string email, string nd)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "delete from GopY where MaSo_TGY = @mstgy";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@mstgy", idtgy);
            cmd.Parameters.AddWithValue("@HoTen_NGY", htngy);
            cmd.Parameters.AddWithValue("@DiaChi_NGY", dc);
            cmd.Parameters.AddWithValue("@SDT_NGY", sdt);
            cmd.Parameters.AddWithValue("@Email_NGY", email);
            cmd.Parameters.AddWithValue("@NoiDung_NGY", nd);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void buttonXoa_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                deleteGopY(textBoxMaSo_TGY.Text, textBoxHoTen_NGY.Text, textBoxDiaChi_NGY.Text, textBoxSDT_NGY.Text, textBoxEmail_NGY.Text, textBoxNoiDung_NGY.Text);
                textBoxMaSo_TGY.Text = "";
                textBoxHoTen_NGY.Text = "";
                textBoxDiaChi_NGY.Text = "";
                textBoxSDT_NGY.Text = "";
                textBoxEmail_NGY.Text = "";
                textBoxNoiDung_NGY.Text = "";
                LoadDataGridViewGopY();
            }
        }

        private void Auto_Load()
        {
            int i = dataGridViewGopY.SelectedRows[0].Index;
            string idtgy = dataGridViewGopY.Rows[i].Cells["MaSo_TGY"].Value.ToString();

            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from GopY where MaSo_TGY = @mstgy";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@mstgy", idtgy);

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                textBoxMaSo_TGY.Text = reader["MaSo_TGY"].ToString();
                textBoxHoTen_NGY.Text = reader["HoTen_NGY"].ToString();
                textBoxDiaChi_NGY.Text = reader["DiaChi_NGY"].ToString();
                textBoxSDT_NGY.Text = reader["SDT_NGY"].ToString();
                textBoxEmail_NGY.Text = reader["Email_NGY"].ToString();
                textBoxNoiDung_NGY.Text = reader["NoiDung_NGY"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void dataGridViewGopY_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Auto_Load();
        }
    }
}
